package org.com.gamemall.Utity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisCacheTime {
    public static final int CACHE_TIME = 60 * 60 * 24;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public  <T> T Addcachewithtime(String key, Class<T> clazz, long time) {
        T value= (T) stringRedisTemplate.opsForValue().get(key);
        if (value != null){
            stringRedisTemplate.expire(key,time, TimeUnit.MINUTES);
        }
        return value;
    }

    /**
     * 通过检查用户是否在线 刷新缓存时间
     * @param key
     * @param time
     * @return
     */
    public Boolean Refreshtime(String key, long time) {
        Boolean exsist = stringRedisTemplate.hasKey(key);
        if (exsist){
            stringRedisTemplate.expire(key, time, TimeUnit.MINUTES);
       return true;
        }
        return false;

    }
}
